﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using AutoMapper;

namespace Data
{
   public class ChiTietBaoCaoTonRepository
    {
       private static ChiTietBaoCaoTonDTO MapChiTietBaoCaoTonEFToBaoCaoTonDTO(CHITIETBAOCAOTON chitietbaocaotonEF)
       {
           Mapper.CreateMap<CHITIETBAOCAOTON, ChiTietBaoCaoTonDTO>();
           return Mapper.Map<CHITIETBAOCAOTON, ChiTietBaoCaoTonDTO>(chitietbaocaotonEF);
       }
       private static CHITIETBAOCAOTON MapChiTietBaoCaoTonDTOToBaoCaoTonEF(ChiTietBaoCaoTonDTO chitietbaocaotonDTO)
       {
           Mapper.CreateMap<ChiTietBaoCaoTonDTO, CHITIETBAOCAOTON>();
           return Mapper.Map<ChiTietBaoCaoTonDTO, CHITIETBAOCAOTON>(chitietbaocaotonDTO);
       }
       public static List<ChiTietBaoCaoTonDTO> GetChiTietBaoCaoTonByMaBaoCaoTon(int mabaocaoton)
       {
           List<ChiTietBaoCaoTonDTO> list = new List<ChiTietBaoCaoTonDTO>();
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   var query = from chitietbaocaoton in context.CHITIETBAOCAOTONList
                               where chitietbaocaoton.MaBaoCaoTon == mabaocaoton
                               select chitietbaocaoton;
                   foreach (var chitietbaocaotonEF in query)
                   {
                       ChiTietBaoCaoTonDTO chitietbaocaotonDTO = MapChiTietBaoCaoTonEFToBaoCaoTonDTO(chitietbaocaotonEF);
                       list.Add(chitietbaocaotonDTO);
                   }

               }
               catch
               {

               }
           }
           return list;
       }
       public void Insert(ChiTietBaoCaoTonDTO chitietbaocaotonDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   CHITIETBAOCAOTON chitietbaocaotonEF = new CHITIETBAOCAOTON();
                   chitietbaocaotonEF = MapChiTietBaoCaoTonDTOToBaoCaoTonEF(chitietbaocaotonDTO);
                   context.AddToCHITIETBAOCAOTONList(chitietbaocaotonEF);
                   context.SaveChanges();
               }
               catch(Exception)
               {
               }
           }
       }
       public void Update(ChiTietBaoCaoTonDTO chitietbaocaotonDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   EntityKey key = context.CreateEntityKey("CHITIETBAOCAOTONList", new CHITIETBAOCAOTON { MaChiTietBaoCaoTon = chitietbaocaotonDTO.MaChiTietBaoCaoTon });
                   CHITIETBAOCAOTON origchitietbaocaoton = context.GetObjectByKey(key) as CHITIETBAOCAOTON;
                   context.Detach(origchitietbaocaoton);
                   context.Attach(origchitietbaocaoton);
                   CHITIETBAOCAOTON updatechitietbaocaoton = MapChiTietBaoCaoTonDTOToBaoCaoTonEF(chitietbaocaotonDTO);
                   context.ApplyCurrentValues<CHITIETBAOCAOTON>("CHITIETBAOCAOTONList", updatechitietbaocaoton);
                   context.SaveChanges();
               }
               catch(Exception)
               {
               }
           }
       }
    }
}
